package com.fulihui.systemcore.service;

import com.fulihui.systemcore.api.WechatConfigService;
import com.fulihui.systemcore.condition.WechatConfigCondition;
import com.fulihui.systemcore.dto.WechatConfig;
import com.fulihui.systemcore.repository.WechatConfigRepository;
import org.apache.dubbo.config.annotation.Service;
import org.near.servicesupport.error.CommonServiceErrorEnum;
import org.near.servicesupport.request.TRequest;
import org.near.servicesupport.result.ResultBuilder;
import org.near.servicesupport.result.TMultiResult;
import org.near.servicesupport.result.TSingleResult;
import org.near.servicesupport.util.ServiceAssert;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 *
 * @author Created by Willard.Hu on 2016/6/2.
 */
@Service(version = "1.0.0")
public class WechatConfigServiceImpl implements WechatConfigService {
    @Autowired
    private WechatConfigRepository wechatConfigRepository;

    @Override
    public TSingleResult<WechatConfig> querySingle(TRequest<WechatConfigCondition> request) {
        WechatConfigCondition condition = request.getCondition();
        ServiceAssert.notNull(condition, CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        ServiceAssert.notBlank(condition.getConfigCode(),
            CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        ServiceAssert.notNull(condition.getEnvType(),
            CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        WechatConfig wechatConfig = wechatConfigRepository.querySingle(condition.getConfigCode(),
            condition.getEnvType());
        return ResultBuilder.succTSingle(wechatConfig);
    }

    @Override
    public TMultiResult<WechatConfig> query(WechatConfigCondition condition) {
        ServiceAssert.notNull(condition, CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        List<WechatConfig> wechatConfigs = wechatConfigRepository.query(condition);
        return ResultBuilder.succTMulti(wechatConfigs);
    }
}
